<?php

class ProductUpdateTax extends ImportBase
{
	private $product_table = 'products';
	private $products_description = 'products_description';
	private $products_categories = 'products_to_categories';
	
	private $website_id;
	
	private $default_product_type = 'simple';
	private $default_tax_class;
	private $default_attribute_set;
	
	protected $db;
	
	private $api;
	
	public function __construct()
	{
		global $config;
		//echo "{$config['db_host']},{$config['db_user']},{$config['db_pass']},{$config['db_name']}";
		$this->set_s_db( $config['db_host'],$config['db_user'],$config['db_pass'],$config['db_name'] );
		
		$this->api = Mage::getModel('catalog/category_api');
		
		$this->website_id = $config['website_id'];
		
		$this->default_tax_class = $config['default_tax_class'];
		$this->default_attribute_set = $config['default_attribute_set'];
		
		$this->db = Mage::getSingleton ( 'core/resource' )->getConnection ( 'core_write' );
		
		parent::__construct();
	}
	
	function run()
	{
		$all_products = $this->getAllProduct();
		foreach( $all_products as $_product ){
			echo 'going to import '.$_product['new_product_id'].'<br />';
			$this->import_product( $_product['new_product_id'],$_product['products_id'] );
			echo 'imported <br>';
			$this->transation_commit();
		}
	}
	
	protected function getAllProduct()
	{
		$query = "select p.*,log.product_id as new_product_id from `{$this->product_table}` p left join `{$this->log_table}` log ON p.products_id = log.org_product_id where log.org_product_id is not null and log.tax_class is null";
		
		$this->s_db->query( $query );
		echo $this->s_db->num_rows().' total need to import.';
		return $this->s_db->af();
	}
	
	
	private function import_product( $product_id,$os_product_id )
	{
		static $counter = 0;
		global $config;
		
		$product_data = $this->read_product( $os_product_id );
		
		echo 'Try to read product from magento.(product id:'.$product_id.')<br />';
		$updateProduct = Mage::getModel( 'catalog/product' )->load( $product_id );
		echo 'read ok.(product id:'.$product_id.')<br />';
		
		if( $product_data['products_tax_class_id'] && isset( $config['tax_class_match'][ $product_data['products_tax_class_id'] ] ) ){
		
			echo 'try to update Tax Class = '.$config['tax_class_match'][ $product_data['products_tax_class_id'] ].' from '.$product_data['products_tax_class_id'].'<br>';
			$updateProduct->addAttributeUpdate ( 'tax_class_id',$config['tax_class_match'][ $product_data['products_tax_class_id'] ],0 );
			$status = $product_data['products_status']?1:2;
			$updateProduct->addAttributeUpdate ( 'status',$status,0 ); //update status
			$this->s_db->query( "update `{$this->log_table}` set tax_class = '{$config['tax_class_match'][ $product_data['products_tax_class_id'] ]}' where product_id = '{$product_id}'" );	
		}
		echo 'product_id = '.$product_id.'<br />';
		echo $updateProduct->getId().'<br />';
		
		
	}
	
	
	public function read_product( $product_id )
	{
		global $config;
		$product_data = array();
		
		$store_views = $this->allStoreViews();
		
		$query = "select * from `{$this->product_table}` where products_id = '{$product_id}'";
		
		$this->s_db->query( $query );
		if( $this->s_db->num_rows() ){
			$product_data = $this->s_db->fetch_array();
		}		
		
		return $product_data;
	}
}

